package org.eae.eldf.dao.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import org.eae.eldf.dao.sys.ISysRoleDao;
import org.eae.eldf.dto.sys.SysRoleQueryDto;
import org.eae.eldf.entity.sys.SysPurview;
import org.eae.eldf.entity.sys.SysRole;
import org.eae.eldf.framework.dao.JdbcDaoImpl;
import org.eae.eldf.framework.pager.Pagination;


@Repository
public class SysRoleDaoImpl extends JdbcDaoImpl implements ISysRoleDao {

	@Override
	public Pagination<SysRole> pageRoleList(Pagination<SysRole> pager,
			SysRoleQueryDto search) {
		StringBuffer sql=new StringBuffer("SELECT * FROM SYS_ROLE R ");
		List<Object> params=new ArrayList<Object>();
		if(null!=search){
			if (StringUtils.isNotBlank(search.getSrolename())) {
				sql.append("WHERE LOWER(R.SROLENAME) LIKE ? ");
				params.add("%"+search.getSrolename().toLowerCase()+"%");
			}
			if (StringUtils.isNotBlank(pager.getOrderBy())&& StringUtils.isNotBlank(pager.getSort())) {
				sql.append("ORDER BY "+pager.getOrderBy()+" "+pager.getSort());
			}
			else {
				sql.append("ORDER BY DCREATETIME DESC");
			}
		}
		
		return this.queryListByPage(pager, sql.toString(),params,SysRole.class);
	}

	@Override
	public List<SysPurview> queryPurviewByRole(Long roleid) {
		String sqlString = "SELECT * FROM SYS_PURVIEW T LEFT JOIN SYS_ROLE_PURVIEW RP ON T.SYS_PURVIEW_ID = RP.SYS_PURVIEW_ID WHERE RP.SYS_ROLE_ID = ?";
		return this.queryBySQL(sqlString, new Object[]{roleid}, SysPurview.class);
	}

	
}
